#include "config.h"

static config sys_conf = {0};
static char* file_path = "./config/sysconfig";

static void default_config(void){
    sys_conf.print_code = 0;
    sys_conf.server_port = 5000;
    sys_conf.sample_time = 60;
}

static void load_config(void){
    FILE* fp;
    char buf[256]={0};
    int len,v_num,n=10,index = 0;
    float f_num;
    char name[32]={0};
    char value[64] = {0};

    info infos[] = {
        {"print_code",OFFSET(config,print_code),SIZE(config,print_code),type_num}, 
        {"server_port",OFFSET(config,server_port),SIZE(config,server_port),type_num}, 
        {"sample_time",OFFSET(config,sample_time),SIZE(config,sample_time),type_num},  
    };
    int i,info_size = sizeof(infos)/sizeof(infos[0]);
    fp = fopen(file_path,"r");
    if(fp == NULL){
        printf("load config failed!\n");
        default_config();
        return;
    }
    
    while(fgets(buf,256,fp) != NULL){
        len = strlen(buf);
        if(find_key("^ *#",buf,NULL,NULL,0,0)) {
            continue;
        }        
        if(find_keys("([0-9a-z_]+) *= *([0-9a-z_ \\./]+)",buf,2,name,value)){
            for(i=0;i<info_size;i++){
                if(strcmp(infos[i].name,name) == 0){                
                    if(infos[i].type == type_num){
                        if(find_key("\\.",buf,NULL,NULL,0,0)){
                            f_num = strtof(value,NULL);
                            memcpy((char*)&sys_conf+infos[i].offset,&f_num,infos[i].size);
                        }
                        else{
                            if(strncmp(value,"0x",2) == 0 || strncmp(value,"0X",2) == 0){
                                n = 16;
                                index = 2;
                            }
                            else if(strncmp(value,"B",1) == 0 || strncmp(value,"b",1) == 0){
                                n = 2;
                                index = 1;
                            }
                            else{
                                n = 10;
                                index = 0;
                            }
                            v_num = strtol(value+index,NULL,n);
                            memcpy((char*)&sys_conf+infos[i].offset,&v_num,infos[i].size);
                        }                                                                        
                    }
                    else{
                        memcpy((char*)&sys_conf+infos[i].offset,value,infos[i].size);
                    }
                }
            }
        }
    } 
    fclose(fp);
}

void init_config(){
    load_config();
}

void set_print_code(int flag,int value){
    if(value) sys_conf.print_code |= (1<<flag);
    else sys_conf.print_code &= ~(1<<flag);
}

uint32_t get_print_code(void){
    return sys_conf.print_code;
}

void reset_print_flag(void){
    sys_conf.print_code = 0;
}

uint16_t get_server_port(void){
    return sys_conf.server_port;
}

uint32_t get_sample_time(void){
    return sys_conf.sample_time;
}